<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Models\PolicySupport;
use App\Admin\Repositories\PolicyFile;
use App\Models\PolicyFile as ModelsPolicyFile;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PolicyFileController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PolicyFile(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('cover', '封面图')->image('', 120, 60);
            $grid->column('description')->limit(40);
            $grid->column('status')->using([0 => '失效', 1 => '有效'])->label([
                0 => 'gray',
                1 => 'green'
            ]);
            $grid->column('updated_at')->sortable();

            $grid->actions( function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(ModelsPolicyFile::class));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PolicyFile(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('cover', '封面图')->image();
            $show->field('description')->unescape();
            $show->field('file', '政策文件')->file();
            $show->field('content', '文件内容')->unescape();
            $show->field('status')->using([0 => '失效', 1 => '有效'])->label('green');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make( new PolicyFile(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->image('cover', '封面图')->autoUpload()->saveFullUrl();
            $form->textarea('description');
            $form->radio('type')->options([0 => '文件', 1 => '内容'])->default(0)
                ->when(0, function (Form $form) {
                    $form->file('file', '政策文件')->autoUpload()->saveFullUrl();
                })
                ->when(1, function (Form $form) {
                    $form->editor('content', '文件内容');
                });
            $form->multipleSelect('support_id', '相关政策')->rules('nullable')
                ->options(PolicySupport::all()->pluck('titile', 'id'))
                ->saving(function ($v) {
                    return implode(',', $v);
                });
            $form->radio('status')->options([0 => '失效', 1 => '有效'])->default(1);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
